<template>
  <x-dialog :title="title" :show.sync="visible" v-if="visible" :before-close="handleClose" width="600">
    <el-form :model="params" class="demo-form-inline" ref="params" :rules="rules" label-position="top">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('config.material.code')" prop="materialCode">
            <el-input
              v-model="params.materialCode"
              :placeholder="$t('config.material.codePlaceholder')"
              clearable
              maxlength="100"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('config.material.name')" prop="materialName">
            <el-input
              v-model="params.materialName"
              :placeholder="$t('config.material.namePlaceholder')"
              clearable
              maxlength="100"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('config.material.specs')" prop="specs">
            <el-input
              v-model="params.specs"
              :placeholder="$t('config.material.specsPlaceholder')"
              clearable
              maxlength="100"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('config.materialType.name')" prop="materialTypeId">
            <x-select-tree
              v-model="params.materialTypeId"
              ref="selectTree"
              :options="treeData"
              :placeholder="$t('config.materialType.namePlaceholder')"
              :defaultProps="treeProps"
              disabled
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('config.material.unit')" prop="unit">
            <x-select
              :placeholder="$t('config.material.unitPlaceholder')"
              v-model="params.unit"
              :options="dictOptions['srm_material.material_unit']"
              style="width: 100%"
            ></x-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('config.material.qualityFlag')" prop="qualityFlag">
            <x-radio-group :list-data="dictOptions['system.preset']" v-model="params.qualityFlag"></x-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('config.material.state')" prop="state">
            <x-radio-group :list-data="dictOptions['system.state']" v-model="params.state"></x-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('config.material.remark')" prop="remark">
            <el-input
              v-model="params.remark"
              type="textarea"
              :rows="3"
              maxlength="1000"
              :placeholder="$t('config.material.inputPlaceholder')"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button @click="handleClose"> {{ $t('common.cancel') }} </el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading"> {{ $t('common.confirm') }} </el-button>
    </span>
  </x-dialog>
</template>
<script>
  import { cloneDeep, omit, throttle } from '@package/utils'
  import { apiEditMaterial, apiAddMaterial } from '../api/material'
  import { EnumApiCode } from '../../../../../../globalEnum.js'

  export default {
    name: 'MaterialAdd',
    props: {
      treeData: {
        type: Array,
        default: () => []
      },
      treeProps: {
        type: Object,
        default: () => ({})
      },
      dictOptions: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      return {
        visible: false,
        loading: false,
        params: {
          materialTypeId: '0',
          materialName: '',
          materialCode: '',
          remark: '',
          state: '1',
          specs: '',
          qualityFlag: '1'
        },
        rules: {
          materialTypeId: [
            { required: true, message: this.$t('config.materialType.upNamePlaceholder'), trigger: 'blur' }
          ],
          materialName: [
            { required: true, message: this.$t('config.materialType.namePlaceholder'), trigger: 'blur' },
            { min: 1, max: 100, message: this.$t('config.materialType.lengthTip'), trigger: 'blur' }
          ],
          materialCode: [
            { required: true, message: this.$t('config.materialType.codePlaceholder'), trigger: 'blur' },
            { min: 1, max: 100, message: this.$t('config.materialType.lengthTip'), trigger: 'blur' }
          ],
          unit: [{ required: true, message: this.$t('config.material.unitPlaceholder'), trigger: 'change' }],
          state: [{ required: true, message: this.$t('config.materialType.selectPlaceholder'), trigger: 'change' }],
          qualityFlag: [
            { required: true, message: this.$t('config.materialType.selectPlaceholder'), trigger: 'change' }
          ]
        }
      }
    },
    methods: {
      handleOpen(info) {
        this.visible = true
        this.params.materialTypeId = info?.materialTypeId ?? 0
        this.title = info?.id ? this.$t('config.material.editTitle') : this.$t('config.material.addTitle')
        this.getInfo(info)
        this.title = this.$t('config.material.addTitle')
      },
      getInfo(info) {
        this.$nextTick(() => {
          this.params = Object.assign(this.params, cloneDeep(info))
          this.params.state += ''
          this.params.qualityFlag += ''
        })
      },
      handleSubmit: throttle(function () {
        this.$refs.params.validate(async (valid) => {
          if (valid) {
            const param = {
              ...this.params
            }
            const { code, msg } = 'id' in this.params ? await apiEditMaterial(param) : await apiAddMaterial(param)
            if (code === EnumApiCode.Success) {
              this.$message.success(param.id ? this.$t('common.editSuccess') : this.$t('common.newSuccess'))
              this.$emit('handleSuccess')
              this.handleClose()
            } else {
              this.$message.error(msg)
            }
          } else {
            return false
          }
        })
      }, 1000),
      handleClose() {
        this.$refs.params.resetFields()
        this.params = omit(this.params, 'id')
        this.visible = false
      }
    }
  }
</script>
